package com.xyzisme76.application;

import com.xyzisme76.dao.ScDao;
import com.xyzisme76.dao.StudentDao;
import com.xyzisme76.pojo.Sc;
import com.xyzisme76.pojo.Student;
import com.xyzisme76.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class SqlSelectInfo {
    private static final int WIDTH =500;
    private static final int HEIGHT =400;
    private static int selectFunc = 0;
    SqlSelectInfo(){
        sqlSelectObm();
    }
    void sqlSelectObm(){
        JFrame jf = new JFrame("sql");


        jf.setSize(new Dimension(WIDTH,HEIGHT));
        //jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //测试时方便快速退出

        // 所在位置 居中
        jf.setLocationRelativeTo(null);
        //设置背景图片
        JPanel jp = new JPanel() {
            protected void paintComponent(Graphics g) {
                try {
                    Image bg = ImageIO.read(new File(
                            "src/main/java/com/xyzisme76/application/images/page3.PNG"));
                    g.drawImage(bg, 0, 0, getWidth(), getHeight(), null);
                }catch(IOException e) {
                    e.printStackTrace();
                }
            }
        };
        JButton jSno = new JButton("信息查询");
        JButton jCon = new JButton("学分查询");
        JButton jGrade = new JButton("成绩查询");
        JTextArea jta = new JTextArea("请选择您要查询的方式");
        JTextField jtf = new JTextField(20);
        JTextArea infor = new JTextArea();
        JButton seach = new JButton("查询");

        jta.setForeground(Color.DARK_GRAY);
        jta.setBackground(Color.white);
        jta.setFont(new Font("微软雅黑", Font.BOLD, 15));
        jCon.setForeground(Color.DARK_GRAY);
        jCon.setBackground(Color.white);
        jCon.setFont(new Font("微软雅黑", Font.BOLD, 20));
        jSno.setForeground(Color.DARK_GRAY);
        jSno.setBackground(Color.white);
        jSno.setFont(new Font("微软雅黑", Font.BOLD, 20));
        jGrade.setForeground(Color.DARK_GRAY);
        jGrade.setBackground(Color.white);
        jGrade.setFont(new Font("微软雅黑", Font.BOLD, 20));
        seach.setForeground(Color.DARK_GRAY);
        seach.setBackground(Color.white);
        seach.setFont(new Font("微软雅黑", Font.BOLD, 15));
        infor.setForeground(Color.DARK_GRAY);
        infor.setBackground(Color.white);
        infor.setFont(new Font("微软雅黑", Font.BOLD, 16));

        jCon.addActionListener(e -> {
            jta.setText("学分查询  请输入您的学号或者姓名");
            selectFunc = 1;
            infor.setText("");
        });
        jSno.addActionListener(e -> {
            jta.setText("信息查询  请输入您的学号或者姓名");
            selectFunc = 3;
            infor.setText("");
        });
        jGrade.addActionListener(e -> {
            jta.setText("成绩查询 请输入您的学号");
            selectFunc = 2;
            infor.setText("");
        });

        seach.addActionListener(e -> {
            infor.setText("");
            String searchIn =  jtf.getText();
            System.out.println(searchIn);
            if(selectFunc == 2){
                List<Sc> scList =  GetGrade.getIt(searchIn);
                for (Sc student :scList){
                    infor.append("课号" + student.getCno()+"   ");
                    infor.append("成绩" + student.getGrade() + "\n");
                }
            }
            //输出学生查询
            if(selectFunc == 3){
               Student student = (Student) GetInfomation.getSt(searchIn);
               infor.append("学号: " + student.getSno() + "\n"+ "姓名: " + student.getName() + "\n"
                            + "性别: " + student.getSex() + "\n"+ "年龄: " + student.getAge()
                             + "\n"+ "所在系: " + student.getDept());
            }
        });




        jp.add(seach);
        jp.add(jta);
        jp.add(jtf);
        jp.add(infor);
        jp.add(jCon);
        jp.add(jGrade);
        jp.add(jSno);
        jf.add(jp);

        jp.setLayout(null);
        //布局太麻烦了，直接绝对位置
        seach.setBounds(360,120,80,30);
        jCon.setBounds(70,10,120,30);
        jGrade.setBounds(200,10,120,30);
        jSno.setBounds(330,10,120,30);
        jta.setBounds(70, 70, 400, 30);
        jtf.setBounds(70,120,290,30 );
        infor.setBounds(100,160,400,300);
        jf.setVisible(true);

    }
}

class  GetGrade{
    static List<Sc> getIt(String Sno){

        SqlSession sqlSession = MybatisUtil.getSqlSession();
        ScDao scDao = sqlSession.getMapper(ScDao.class);
        List<Sc> scList = scDao.getGradeBySno(Sno);
        for (Sc student :scList){
            System.out.println(student);
        }
//        Sc sc = scDao.getGradeBySno(sno);
        sqlSession.close();
        return scList;
    }
}

class GetInfomation {
    static Student getSt(String Sno){
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        Student student = studentDao.getStudentBySno(Sno);
        System.out.println(student);
//        Sc sc = scDao.getGradeBySno(sno);
        sqlSession.close();
        return student;
    }
}


